import requests
import json
import re
import os
import sys
from lxml import etree

cwd = os.getcwd()
sys.path.append(cwd)
print(cwd)

class ImageMijisou(object):
    url = 'https://mijisou.com/'
    # 匹配图片url的xpath，如：https://tse1-mm.cn.bing.net/th/id/OIP.73Ob4lcxFaLaxTKil_dnJQAAAA?w=190&h=143&c=7&o=5&pid=1.7
    img_url_xpath = '//*[@id="main_results"]/div/a/img/@src'
    save_dir = './mijisou' # 文件保存的路径
    count = 0

    # 初始化
    def __init__(self, word):
        self.word = word
        self.dir_path = os.path.join(self.save_dir, word)
        self.params = {
            'q': '黑色牌照', # 搜索关键字
            'category_images': 'on',
            'time_range': '', # 时间限制
            'language': 'zh-CN',
            'pageno': '1', # 页数
        }
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
        }
        # 创建保存图片的文件夹
        self.folder_exist(self.dir_path)
        self.current_page = 1 # 当前页数
    
    # 请求包含图片url的网页
    def parse(self):
        while True:
            self.params['pageno'] = str(self.current_page)
            try:
                response = requests.get(url=self.url, headers=self.headers, params=self.params)
            except Exception as e:
                print('请求页面失败')
                continue
            response.encoding = 'utf8'
            if response.status_code == 200:
                html = etree.HTML(response.text)
                list_img_url = html.xpath(self.img_url_xpath)
                # 判断是否有图片,没有则结束下载
                if not list_img_url:
                    break
                for img_url in list_img_url:
                    self.download(img_url)
                self.current_page += 1
            else:
                break

    # 下载一张图片
    def download(self, img_url, img_type='jpg'):
        self.count += 1
        print('正在下载第%d张图片...'%self.count, img_url)
        try:
            response = requests.get(img_url, headers=self.headers)
        except Exception as e:
            print('第%d张图片...下载失败,'%self.count, img_url)
        img_name = img_url.split('/')[-1]
        img_path = os.path.join(self.dir_path, img_name)
        if not img_path.endswith('jpg') or not img_path.endswith('png') or not img_path.endswith('jpeg') or not img_path.endswith('gif'):
            img_path = img_path + '.jpg'
        if os.path.exists(img_path):
            print('图片已存在,', img_path)
            return None
        img_path.replace('?', '')
        with open(img_path, 'wb') as f:
            f.write(response.content)

    def folder_exist(self, dir_path):
        '''
        1. 作用:判断文件夹路径是否存在,不存在则创建
        2. 参数:dir_path:文件夹路径
        3. 返回值:None
        '''
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

if __name__ == '__main__':
    image = ImageMijisou('黑色牌照')
    image.parse()